package com.lechi.web.framework.util;

import com.google.common.collect.*;
import com.lechi.web.system.domain.IotMonitorDataBo;
import com.lechi.web.system.domain.IotWeatherSensorParseBo;
import lombok.Getter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class GuavaCacheUtil {


    /**
     * 启动状态
     */
    @Getter
    public final static String STARTED = "1";


    /**
     * 关闭状态
     */
    @Getter
    public final static String STOPED = "0";

    @Getter
    private final static Map<String, Date> deviceLastDated = new ConcurrentHashMap();

    @Getter
    private final static Map<String, Boolean> stateCodeMap = new ConcurrentHashMap();

    public static String ifTimeOut(String deviceIdOrSN, int timeInternal) {
        return Optional.ofNullable(deviceLastDated.get(deviceIdOrSN))
                .map(endTime -> new Date().getTime() - endTime.getTime() > timeInternal * 60000L ? getSTOPED() : getSTARTED()).orElse(getSTOPED());
    }

    /**
     * Guava的BiMap类提供了双向映射的功能，即可以通过键获取值，也可以通过值获取键。 bidirectional
     *
     * 若键已存在：新的值会覆盖旧的值，并且会自动移除旧值对应的反向映射。
     * 若值已存在：会抛出 IllegalArgumentException 异常，因为 BiMap 不允许值重复。
     */
    @Getter
    private final static BiMap<String, String> biDeviceSnAndIPMap = HashBiMap.create();

    @Getter
    private final static Map<Long, String> deviceIdAndIPMap = new ConcurrentHashMap();

    @Getter
    private final static BiMap<String, String> deviceIdAndStationMap = HashBiMap.create();


    @Getter
    private final static Map<Long, String> deviceIdAndInstrumentIdMap = new HashMap();

    /**
     *Table类允许您创建一个类似于二维表格的映射，其中每个键都可以映射到多个值。
     *
     */
    @Getter
    private final static Map<String, String> deviceTableMap = new ConcurrentHashMap();

    @Getter
    private final static Map<String, List<?>> cmdParseTableMap = new ConcurrentHashMap();

    @Getter
    private final static Map<String, Set<Long>> alarmSensorMap = new ConcurrentHashMap();

    @Getter
    private final static Map<String, List<Map<String, String>>> scheduleSendCmdMap = new ConcurrentHashMap();

    @Getter
    private final static Map<String, List<?>> AlarmRuleMap = new ConcurrentHashMap();

    @Getter
    private final static Table<Long, Long, IotMonitorDataBo> monitorDataMap = HashBasedTable.create();


    @Getter
    private final static Map<String, Map<String, IotWeatherSensorParseBo>> weatherParseTableMap = new ConcurrentHashMap();


    @Getter
    private final static Map<Integer, String> sH_A_Map = Map.ofEntries(
            Map.entry(0, "DEPTH"),
            Map.entry(1, "SPEED"),
            Map.entry(2, "WSPDA"),
            Map.entry(3, "WDIRA"),
            Map.entry(4, "TEMPA"),
            Map.entry(5, "HUMIA"),
            Map.entry(6, "PRESA"),
            Map.entry(7, "PRECA"),
            Map.entry(8, "PRECA_p1accu"),
            Map.entry(9, "PRECA_p24accu"),
            Map.entry(10, "PRECA_accu")
    );

    @Getter
    private final static Map<Integer, String> sH_B_Map = Map.of(
            0, "PRECA",
            1, "PRECA_p1accu",
            2, "PRECA_p24accu",
            3, "PRECA_accu",
            4, "SV5",
            5, "SV10",
            6, "SV20",
            7, "SV30",
            8, "SV40"
    );
}
